package com.atguigu.yygh.cmn.controller;


import com.atguigu.yygh.cmn.service.DictService;
import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.model.cmn.Dict;
import com.atguigu.yygh.model.hosp.Hospital;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * <p>
 * 组织架构表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2022-08-23
 */
@RestController
@RequestMapping("/admin/cmn")
// @CrossOrigin // 支持跨域
public class DictController {

    @Autowired
    private DictService dictService;

    @ApiOperation(value = "文件上传")
    @PostMapping("/upload")
    public R upload(MultipartFile file) throws IOException {
        // 调用Service层方法处理文件上传
        dictService.upload(file);
        return R.ok();
    }

    @ApiOperation(value = "excel文件下载")
    @GetMapping("/download")
    public void download(HttpServletResponse response) throws IOException {
        dictService.downloadExcel(response);
    }

    @ApiOperation(value = "根据父id查询数据字典接口")
    @GetMapping("/getChildList/{pid}")
    public R getChildList(@ApiParam(name = "pid", value = "父id") @PathVariable("pid")Long pid) {
        List<Dict> list = dictService.getChildListByParentId(pid);
        return R.ok().data("items", list);
    }

    @ApiOperation(value = "提供的微服务远程接口-根据医院省市编码获取文字")
    @GetMapping("/{value}")
    public String getNameByValue(@PathVariable("value") Long value) {
        // 根据value获取文字信息
        return dictService.getNameByValue(value);
    }

    @ApiOperation(value = "提供的微服务远程接口-根据医院等级编码获取文字")
    @GetMapping("/{dictCode}/{value}")
    public String getNameByDictCodeAndValue(@PathVariable("dictCode")String dictCode,
                                            @PathVariable("value")Long value) {
        return dictService.getNameByDictCodeAndValue(dictCode, value);
    }
}

